using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace CloudApiServer.Services;

/// <summary>
/// 医保基础服务 
/// </summary>
public class FsiBasicService
{
    /// <summary>
    /// 1201 定点机构查询
    /// </summary>
    /// <param name="fsiRequest">入参</param>
    /// <param name="ControllerName">来源控制器名称</param>
    /// <returns>出参</returns>
    /// <exception cref="Exception"></exception>
    public async Task<OutputQueryFixMedIns> QueryFixMedIns(FsiRequestHeader<InputQueryFixMedIns> fsiRequest, string ControllerName)
    {
        try
        {
            string fixmedins_code = fsiRequest.Input.medinsinfo.fixmedins_code;
            string fixmedins_name = fsiRequest.Input.medinsinfo.fixmedins_name;
            string fixmedins_type = fsiRequest.Input.medinsinfo.fixmedins_type;

            if (string.IsNullOrEmpty(fixmedins_type))
            {
                throw new Exception($"参数校验失败：传入的定点医疗服务机构类型不可以为空！请检查入参！");
            }

            if (string.IsNullOrEmpty(fixmedins_code)&& string.IsNullOrEmpty(fixmedins_name))
            {
                throw new Exception($"参数校验失败：传入的定点医药机构编号和定点医药机构名称不能同时为空！请检查入参！");
            }

            List<OutputQueryFixMedInsedinsinfoList> stroeList = new List<OutputQueryFixMedInsedinsinfoList>();
            string connectionString =
                $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
            string query = "select  * From  fsi_queryfixmedins where fixmedins_type =@fixmedins_type ";
            var parameters = new List<MySqlParameter>();

            if (!string.IsNullOrEmpty(fixmedins_code))
            {
                query += " and fixmedins_code like @fixmedins_code";
                parameters.Add(new MySqlParameter("@fixmedins_code", $"%{fixmedins_code}%"));
            }

            if (!string.IsNullOrEmpty(fixmedins_name))
            {
                query += " and fixmedins_name like @fixmedins_name";
                parameters.Add(new MySqlParameter("@fixmedins_name", $"%{fixmedins_name}%"));
            }

            query += " order by fixmedins_code ";
            parameters.Add(new MySqlParameter("@fixmedins_type", fixmedins_type));
            DataTable dt = await MySqlHelper.ExecuteQueryAsync(connectionString, query, parameters);

            foreach (DataRow row in dt.Rows)
            {
                var storeinfo = new OutputQueryFixMedInsedinsinfoList()
                {
                    fixmedins_code = row["fixmedins_code"].ToString(),
                    uscc = row["uscc"].ToString(),
                    exp_content = row["exp_content"].ToString(),
                    fixmedins_name = row["fixmedins_name"].ToString(),
                    fixmedins_type = row["fixmedins_type"].ToString(),
                    hosp_lv =row["hosp_lv"].ToString()
                };
                stroeList.Add(storeinfo);
            }

            return new OutputQueryFixMedIns()
            {
                medinsinfo = stroeList
            };
        }
        catch (Exception ex)
        {
            LogHelper.Error(
                $"[FsiBasicService.QueryFixMedIns]医疗机构数据获取异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
            throw;
        }
    }
}